<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
use app\admin\validate\UserValidate;
use app\admin\model\Index as IndexUser;
class Index extends UserValidate{
    /**
     * @return \think\response\View
     * 首页
     */
    public function index(){
        $index = (new IndexUser)->Index();
        if ($index == null){
            return view();
        }
        $this->assign('index',$index);
        return $this->fetch();
    }
    /**
     * 添加洗车卷
     */
    public function CarWashAdds(){
        $CarWash = Request::post();
        $adds = (new IndexUser)->Adds($CarWash);
        if ($adds !== false){
            $this->error('新增失败');
        }
        $this->redirect('admin/index/index');
    }
    /**
     * 添加分类
     */
    public function CarWashAddCategorys(){
        $userinfo = new IndexUser();
        $category_result = $userinfo->Categorys(Request::get());
        if ($category_result == false){
            $this->error('新增失败');
        }
        $this->success('新增成功');
    }

    /**
     * 添加卷
     */
    public function CarWashAddTo(){
        if (empty($_FILES['vs_picture']['name'])){
            $this->error('请上传图片');
        }
        $file = request()->file('vs_picture');
        $target = $file->rule('md5')->validate(['size'=>300000,'ext'=>'jpg,png,gif'])->move( '../public/image',time());
        if ($target == false){
            $this->error('请检查图片');
        }
        $translation = "/image/" .$target->getsaveName();
        $fenlei_add = (new IndexUser)->Adds(Request::post(),$translation);
        if ($fenlei_add == false){
            $this->error('添加失败,请检查是否有提交参数');
        }
        $this->success('新增成功');
    }
    /**
     * 添加卷--渲染
     */
    public function CarWashAdd(){
        $shop = (new IndexUser)->Shop();
        if ($shop == false) {
            $this->error('请先添加店铺');
        } else {
            $fenlei = (new IndexUser)->Fenlei(input('type'));
            if ( $fenlei == false) {
                $this->error('请先添加分类');
            }
        }
        $this->assign([
            'type'=>input('type'),
            'shop'=>$shop,
            'fenlei'=>$fenlei,
        ]);
        return view();
    }
    /**服务分类管理--渲染
     * @return \think\response\View
     */
    public function CarWash(){
        $type = !empty(input('wa_type')) ? input('wa_type') : 1;
        $lx = [1 =>'car_wash','maintain','painting','tyre'];
        $painting = Db::name('car_volume_store')->where('vs_type',$type)->order('vs_id desc')->paginate(10);
        $this->assign([
            'type'=>input($type),
            'painting'=>$painting,
        ]);
        return $this->fetch($lx[$type]);
    }

    /**
     * 道路救援
     * @return \think\response\View
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public function rescue(){
       $order = Db::name('trailer')->order('id desc')->paginate(10);
        $this->assign('order',$order);
        return view();
    }
    public function rescuede(){
        $id = '';
        empty((int)input('id')) || $id = input('id');
        Db::name('trailer')->where('id',$id)->order('id desc')->delete();
        $this->success('成功');
    }
    /**
     * 添加分类
     */
    public function CarWashAddCategory(){
        $this->assign('type',input('type'));
        return view();
    }

    /**   卷修改
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function modifys(){
        $vs_id = input('vs_id');
        $move = input('move');
        if ($move == '2'){
            Db::name('car_volume_store')->where('vs_id',$vs_id)->delete();
            $this->success('成功');
        }
        $modifys_value = (new IndexUser)->modifys($vs_id,$move);
        if ($modifys_value === false){
            $this->error('参数错误');
        }
        $this->assign('modifys_value',$modifys_value);
        return view();
    }
    public function modifys_two(){
        if (!empty($_FILES['vs_picture']['name'])){
            $file = request()->file('vs_picture');
            if ($file){
                $target = $file->rule('md5')->validate(['size'=>300000,'ext'=>'jpg,png,gif'])->move( '../public/image',time());
                if ($target == false){
                    $this->error('请检查图片');
                }
            }
            $translation = "/image/" .$target->getsaveName();
            $whole = Request::post();
            $whole['vs_picture'] = $translation;
            $value_add = (new IndexUser)->modifys_two($whole);
            if ($value_add == true){
                $this->success('修改成功');
            }
            $this->success($value_add);
        }
        $modifys_two = (new IndexUser)->modifys_two(Request::post());
        if ($modifys_two == false){
            $this->error('参数错误');
        }
        $this->success('成功');
    }
}